<template>
	<view>
		<uni-card :title="hospitalInfo.name" :sub-title="hospitalInfo.address" :extra="hospitalInfo.level"
			:thumbnail="avatar" @click="onClick">
			<text class="uni-body">{{hospitalInfo.description}}</text>
		</uni-card>
		<van-tree-select :items="departmentList" :main-active-index="selectInfo.mainActiveIndex "
			:active-id="selectInfo.activeId " @click-nav="onClickNav" @click-item="onClickItem" />
	</view>
</template>

<script lang="ts" setup>
	import {
		listDepartment
	} from '@/api/department';
	import {
		queryHospital
	} from '@/api/hospital';
import { get7Day } from '@/utils/date';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		ref, unref
	} from 'vue';
	let hospitalId: any = ''
	const hospitalInfo = ref({})
	const avatar = ref('https://nwzimg.wezhan.cn/contents/sitefiles2022/10113300/images/3148097.jpg')
	const departmentList = ref<any>({})
	const selectInfo = ref({
		mainActiveIndex: 0,
		activeId: null,
	})
	const handleList = async () => {

		const res = await listDepartment(hospitalId)
		const {result}  = res

		const len = result.length
		for(let i = 0;i<len;i++){
			const p = result[i]
			p.text = p.deptName
			for(let j = 0;j<p.children.length;j++){
				p.children[j].text = p.children[j].deptName
			}
		}
		departmentList.value = result
	}
	const getHospitalInfo = async () => {
		const {
			result
		} = await queryHospital(hospitalId.id)
		hospitalInfo.value = result
	}
	const onClickNav  = ({detail={}})=>{
		selectInfo.value.mainActiveIndex = detail.index 
	}
	const onClickItem = ({detail={}})=>{
		const activeId = detail.id
		selectInfo.value.activeId = activeId
		const dept1Id = unref(departmentList)[selectInfo.value.mainActiveIndex].id 
		uni.navigateTo({
			url:`/pages/schedule/schedule?dept1Id=${dept1Id}&dept2Id=${activeId}&hospitalId=${hospitalId.id}`
		})
	}
	onLoad((id) => {
		hospitalId = id
		handleList()
		getHospitalInfo()
	})
</script>

<style>

</style>
